package ErChaShu;

import java.util.HashMap;

public class HuffmanCodingTest {
    public static void main(String[] args) {
        // 待编码字符串
        String text = "AAABCCDDDDEEEEEFFFFFF";
        // 计算待编码字符串每一个字符的频次
        HuffmanNode[] nodes = HuffmanTreeUtil.calculateFrequency(text);

        // 构造哈夫曼树
        HuffmanNode root = HuffmanTreeUtil.buildHuffmanTree(nodes);

        // 计算每一个字符的二进制编码序列
        HashMap<Character, String> huffmanCodeMap = new HashMap<>();
        HuffmanTreeUtil.generateCodes(root, "", huffmanCodeMap);
        System.out.println(huffmanCodeMap);

        // 编码字符串
        String encodedText = HuffmanTreeUtil.encodeText(text, huffmanCodeMap);
        System.out.println("Encoded text: " + encodedText);

        // 解码二进制
        String decodedText = HuffmanTreeUtil.decode(encodedText, root);
        System.out.println("Decoded text: " + decodedText);
    }
}

